<template>
  <main-container>
    <search-form class="mb20">
      <search-item>
        <router-link :to="{ name: 'userCreate' }">
          <el-button class="w100" type="primary" icon="el-icon-plus">添加管理员</el-button>
        </router-link>
      </search-item>
      <search-item slot="action">
        <el-button type="text" icon="el-icon-refresh" @click="init">刷新列表</el-button>
      </search-item>
    </search-form>
    <v-table
      ref="table"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      :handleSizeChange="handleSizeChange"
      :handleCurrentChange="handleCurrentChange"
      :data="tableData"
      :columns="columns"
    >
      <el-table-column label="操作" align="center" width="250px">
        <template v-if="scope.row.id !== 1" slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope, 1)">修改密码</el-button>
          <el-button type="text" @click="handleEdit(scope, 2)">修改角色</el-button>
          <el-button type="text" @click="handleDel(scope)">删除</el-button>
        </template>
      </el-table-column>
    </v-table>
  </main-container>
</template>

<script>
import table from '@/common/mixins/table'
import { getUserInfoList, deleteUserInfo } from '@/api/workbench'

export default {
  name: 'userList',
  mixins: [table],
  data() {
    return {
      tableData: [],
      columns: [
        {
          label: '用户名',
          prop: 'realName'
        },
        {
          label: '密码',
          prop: 'password',
          render(h) {
            return h('span', '******')
          }
        },
        {
          label: '手机号',
          prop: 'phone'
        },
        {
          label: '角色',
          prop: 'roleName'
        }
      ]
    }
  },
  computed: {
    userInfo() {
      return this.$store.state.app.userInfo
    }
  },
  created() {
    this.init()
  },
  methods: {
    /**
     * 平台获取管理员列表
     */
    async init() {
      let { page, pageSize } = this
      let res = await getUserInfoList({ page, rows: pageSize })
      let { code, data } = res
      if (code === 0) {
        let { records, userList = [] } = data
        this.total = records
        this.tableData = userList
      }
    },
    /**
     * 修改密码
     */
    handleEdit(e, type) {
      let { id, phone, realName, roleId } = e.row
      this.$router.push({ name: 'userEdit', query: { id, phone, realName, roleId, type } })
    },
    /**
     * 删除角色
     */
    handleDel(e) {
      let { id } = e.row
      this.$confirm('确定删除该管理员？', {
        title: '提示',
        type: 'warning'
      })
        .then(async () => {
          let res = await deleteUserInfo({ userId: id })
          let { code, msg: message } = res
          if (code === 0) {
            this.$message.success(message)
            this.init()
          }
        })
        .catch(() => {})
    }
  }
}
</script>
